function [min_scale,max_scale,min_scalem,max_scalem,min_scalef,max_scalef] = get_scale(singlefemale,singlemale,couple,leisurem,leisuref, hworkm, hworkf, consumption,wagem,wagef,nonlabor,divorce_costsM,divorce_costsF,divorce_costsMF,ub)

% LP to get bounds on a) economies of scale and individual RICEBs
% Program uses YALMIP (https://yalmip.github.io) connected to CPLEX or GUROBI

% INPUT VARIABLES:
% leisurem[households]      -- leisure per week of the male
% leisuref[households]      -- leisure per week of the female
% hworkm[households]        -- time spend on home production by the male
% hworkf[households]        -- time spend on home production by the female
% consumption[households]   -- weekly Hicksian consumption
% wagem[households]         -- wage rate of the male
% wagef[households]         -- wage rate of the female
% nonlabor[households]      -- weekly nonlabor income of the household
% ub                        -- upper bound on economies of scale of leisure (aleisure)
% DivorceCostsM             -- Divorce Costs for individual rationality of males
% DivorceCostsF             -- Divorce Costs for individual rationality of females
% DivorceCostsMF            -- Divorce Costs for no blocking pairs 

% OUTPUT VARIABLES:
% min_scale                 -- minimum economies of scale
% max_scale                 -- maximum economies of scale
% min_scalem                -- minimum RICEB male
% max_scalem                -- maximum RICEB male
% min_scalef                -- minimum RICEB female
% max_scalef                -- maximum RICEB female

%%
%defining constants:
workh = 112;                                          % total potential working hours
num_of_couples = length(find(couple));                % number of couples in the data set
num_of_hh = length(couple);                           % total number of households in the data.
e = 0.000001;

% defining decision variables
privateqM = sdpvar(num_of_hh,1);                      % private consumption of Hicksian good by male
privateqF = sdpvar(num_of_hh,1);                      % private consumption of Hicksian good by female  
privatemhworkm = sdpvar(num_of_hh,1);                 % private consumption of male's domestic work by male
privatemhworkf = sdpvar(num_of_hh,1);                 % private consumption of male's domestic work by female
privatefhworkm = sdpvar(num_of_hh,1);                 % private consumption of female's domestic work by male
privatefhworkf = sdpvar(num_of_hh,1);                 % private consumption of female's domestic work by female  
privateleisurem = sdpvar(num_of_hh,1);                % private consumption of leisure by male
privateleisuref = sdpvar(num_of_hh,1);                % private consumption of leisure by female

scale = sdpvar(num_of_couples,1);                     % economies of scale
scalem = sdpvar(num_of_couples,1);                    % male RICEB
scalef = sdpvar(num_of_couples,1);                    % female RICEB

% Barten scales for couples
aleisurem = sdpvar(1);                                % degree of publicness in leisure of male for couples
aleisuref = sdpvar(1);                                % degree of publicness in leisure of female for couples
ahworkm = sdpvar(1);                                  % degree of publicness in domestic work of male for couples
ahworkf = sdpvar(1);                                  % degree of publicness in domestic work of female for couples
aq = sdpvar(1);                                       % degree of publicness in Hicksian good for couples   

% Barten scales for singles
bleisurem = sdpvar(1);                                % degree of publicness in leisure of male for singles 
bleisuref = sdpvar(1);                                % degree of publicness in leisure of female for singles 
bhworkm = sdpvar(1);                                  % degree of publicness in domestic work of male for singles
bhworkf = sdpvar(1);                                  % degree of publicness in domestic work of female for singles
bq = sdpvar(1);                                       % degree of publicness in Hicksian good for singles

nl_incomeM = sdpvar(num_of_hh,1);                     % non-labor income of male
nl_incomeF = sdpvar(num_of_hh,1);                     % non-labor income of female

%% Setting up the constraints 
Constraints = [];

% Private consumption of Hicksian good must be balanced 
for i=1:num_of_hh
    if (couple(i) == 1)
    Constraints=[Constraints, ...
        consumption(i)*(1-aq) == privateqM(i) + privateqF(i), privateqM(i) >=0, privateqF(i) >= 0];
    else
    Constraints=[Constraints, ...
        consumption(i)*(1-bq) == privateqM(i) + privateqF(i), privateqM(i) >=0, privateqF(i) >= 0];
    end
end

for i=1:num_of_hh
      if singlefemale(i) == 1
            Constraints = [Constraints, ...
                privateqM(i) == 0];
        else
            if singlemale(i) == 1
               Constraints = [Constraints,...
                  privateqF(i) == 0];
            end
      end 
end

% Domestic work by male 
for i=1:num_of_hh
    if (couple(i)==1)
    Constraints=[Constraints, ...
        hworkm(i)*(1-ahworkm) == privatemhworkm(i) + privatemhworkf(i), privatemhworkm(i) >= 0, privatemhworkf(i) >= 0];
    else
        Constraints=[Constraints, ...
        hworkm(i)*(1-bhworkm) == privatemhworkm(i) + privatemhworkf(i), privatemhworkm(i) >= 0, privatemhworkf(i) >= 0];
    end
end

for i=1:num_of_hh
      if singlefemale(i) == 1
            Constraints = [Constraints, ...
                privatemhworkm(i) == 0];             % since hworkm is 0 for single female, this means both privatemhworkm and privatemhworkf are 0 in this case 
      else
            if singlemale(i) == 1
               Constraints = [Constraints,...
                  privatemhworkf(i) == 0];
            end
      end 
end

% Domestic work by female
for i=1:num_of_hh
    if (couple(i)==1)
    Constraints=[Constraints, ...
        hworkf(i)*(1-ahworkf) == privatefhworkm(i) + privatefhworkf(i), privatefhworkm(i) >=0 , privatefhworkf(i) >= 0];
    else
    Constraints=[Constraints, ...
        hworkf(i)*(1-bhworkf) == privatefhworkm(i) + privatefhworkf(i), privatefhworkm(i) >=0 , privatefhworkf(i) >= 0];
    end
end

for i=1:num_of_hh
      if singlefemale(i) == 1
            Constraints = [Constraints, ...
                privatefhworkm(i) == 0];
        else
            if singlemale(i) == 1
               Constraints = [Constraints,...
                  privatefhworkf(i) == 0];        % since hworkf is 0 for single male, this means both privatefhworkm and privatefhworkf are 0 in this case 
            end
      end 
end

% Leisure male
for i=1:num_of_hh
    if(couple(i)==1)
    Constraints=[Constraints, ...
        leisurem(i)*(1-aleisurem) == privateleisurem(i)];
    else
   Constraints=[Constraints, ...
        leisurem(i)*(1-bleisurem) == privateleisurem(i)];
    end
end

% Leisure female
for i=1:num_of_hh
    if (couple(i)==1)
    Constraints=[Constraints, ...
        leisuref(i)*(1-aleisuref) == privateleisuref(i)];
    else
    Constraints=[Constraints, ...
        leisuref(i)*(1-bleisuref) == privateleisuref(i)];
   
    end
end

% balanced nonlabor income:
for i=1:num_of_hh
        if couple(i) == 1
            Constraints = [Constraints, nonlabor(i) == nl_incomeM(i)+nl_incomeF(i)];
        else
            if singlefemale(i) == 1
                Constraints = [Constraints, nonlabor(i) == nl_incomeF(i)];
            else
                if singlemale(i) == 1
                Constraints = [Constraints, nonlabor(i) == nl_incomeM(i)];
                end
            end 
        end
end

% bounding the possible sharing of nonlabor income between partners [.4,.6]
for i=1:num_of_hh
    % check that dealing with couple
    if couple(i) == 1
        if nonlabor(i)>=0
            Constraints = [Constraints,
                .4*nonlabor(i) <= nl_incomeM(i) <= .6*nonlabor(i), .4*nonlabor(i) <= nl_incomeF(i) <= .6*nonlabor(i)];
        else 
            Constraints = [Constraints,
                .6*nonlabor(i) <= nl_incomeM(i) <= .4*nonlabor(i), .6*nonlabor(i) <= nl_incomeF(i) <= .4*nonlabor(i)];
        end
    end
end

% individual rationality
for i=1:num_of_hh
        if (couple(i)==1)
        % male    
        Constraints = [Constraints,
            (wagem(i)*workh)*(divorce_costsM(i)-e) + nl_incomeM(i) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(i) + privateqM(i) + hworkm(i)*ahworkm*wagem(i) + hworkf(i)*ahworkf*wagef(i) + aq*consumption(i) + leisurem(i)*aleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(i)];
        % female
        Constraints = [Constraints,
            (wagef(i)*workh)*(divorce_costsF(i)-e) + nl_incomeF(i) <= privateleisuref(i)*wagef(i) + privatemhworkf(i)*wagem(i) + privatefhworkf(i)*wagef(i) + privateqF(i) + hworkm(i)*ahworkm*wagem(i) + hworkf(i)*ahworkf*wagef(i) + aq*consumption(i) + leisurem(i)*aleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(i)];
        end
end

% no blocking pairs condition
for i=1:num_of_hh
    for j=1:num_of_hh
        if (couple(i)== 1) && (couple(j)==1)
            Constraints = [Constraints, 
                (wagem(i) + wagef(j))*workh*(divorce_costsMF(i,j)-e) + nl_incomeM(i) + nl_incomeF(j) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(j) + privateqM(i) + privateleisuref(j)*wagef(j) + privatemhworkf(j)*wagem(i) + privatefhworkf(j)*wagef(j) + privateqF(j) + max(hworkm(i),hworkm(j))*ahworkm*wagem(i) + max(hworkf(i),hworkf(j))*ahworkf*wagef(j) + aq*max(consumption(i),consumption(j)) + max(leisurem(i),leisurem(j))*aleisurem*wagem(i) + max(leisuref(i),leisuref(j))*aleisuref*wagef(j)];
        end
        if (couple(i)==1) && (singlefemale(j)==1)
           Constraints = [Constraints, 
           (wagem(i) + wagef(j))*workh*(divorce_costsMF(i,j)-e) + nl_incomeM(i) + nl_incomeF(j) <= privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(j) + privateqM(i) + privateleisuref(j)*wagef(j) + privatemhworkf(j)*wagem(i) + privatefhworkf(j)*wagef(j) + privateqF(j) + hworkm(i)*ahworkm*wagem(i) + hworkm(j)*bhworkm*wagem(i) + hworkf(i)*ahworkf*wagef(j)+ hworkf(j)*bhworkf*wagef(j) + aq*consumption(i) + bq*consumption(j) + leisurem(i)*aleisurem*wagem(i) + leisurem(j)*bleisurem*wagem(i) + leisuref(i)*aleisuref*wagef(j)+ leisuref(j)*bleisuref*wagef(j)];
        end
       if (couple(i)==1) && (singlemale(j)==1)
                    Constraints = [Constraints, 
           (wagem(j) + wagef(i))*workh*(divorce_costsMF(j,i)-e) + nl_incomeM(j) + nl_incomeF(i) <= privateleisurem(j)*wagem(j) + privatemhworkm(j)*wagem(j) + privatefhworkm(j)*wagef(i) + privateqM(j) + privateleisuref(i)*wagef(i) + privatemhworkf(i)*wagem(j) + privatefhworkf(i)*wagef(i) + privateqF(i) + hworkm(j)*bhworkm*wagem(j) + hworkm(i)*ahworkm*wagem(j) + hworkf(j)*bhworkf*wagef(i)+ hworkf(i)*ahworkf*wagef(i) + bq*consumption(j) + aq*consumption(i) + leisurem(j)*bleisurem*wagem(j) + leisurem(i)*aleisurem*wagem(j) + leisuref(j)*bleisuref*wagef(i) + leisuref(i)*aleisuref*wagef(i)];
        end
    end
end
   

% bounding the divorce costs
% individual rationality
for i=1:num_of_hh
    if (couple(i)==1)
    Constraints = [Constraints, 0 <= divorce_costsM(i) <= 1, 0 <= divorce_costsF(i) <= 1];
    end
end

% no blocking pairs
for i=1:num_of_hh
    for j=1:num_of_hh
         Constraints = [Constraints,  0 <= divorce_costsMF(i,j) <= 1];
    end
end

for i=1:num_of_hh
    for j=1:num_of_hh
        % bounds on divorce costs work only for m_i and w_j who are in the consideration set of each other
         if (couple(i) == 1) && (singlemale(j) == 1)
             Constraints = [Constraints, divorce_costsMF(i,j) == 0];
         end
         if (singlefemale(i) == 1) && (couple(j) == 1) 
             Constraints = [Constraints, divorce_costsMF(i,j) == 0];
         end
         if (couple(i) == 0) && (couple(j) == 0)
             Constraints = [Constraints, divorce_costsMF(i,j) == 0];
         end
         Constraints = [Constraints,  0 <= divorce_costsMF(i,j) <= 1];
         
    end
end

Constraints = [Constraints,  0 <= aq <= 1, 0 <= aleisurem <= ub, 0 <= aleisuref <= ub, 0 <= ahworkm <= 1, 0 <= ahworkf <= 1];
Constraints = [Constraints,  0 <= bq <= 1, 0 <= bleisurem <= ub, 0 <= bleisuref <= ub, 0 <= bhworkm <= 1, 0 <= bhworkf <= 1];

for i=1:num_of_hh
    if(couple(i)==1)
    Constraints = [Constraints,...
        scale(i) == ((1+aleisurem)*wagem(i)*leisurem(i) + (1+aleisuref)*wagef(i)*leisuref(i) + (1+ahworkm)*wagem(i)*hworkm(i)+ (1+ahworkf)*wagef(i)*hworkf(i) + (1+aq)*consumption(i))/(wagem(i)*workh + wagef(i)*workh + nonlabor(i)),...
        scalef(i) == (privateleisuref(i)*wagef(i) + privatemhworkf(i)*wagem(i) + privatefhworkf(i)*wagef(i) + privateqF(i) + aleisurem*wagem(i)*leisurem(i) +  aleisuref*wagef(i)*leisuref(i) + ahworkm*wagem(i)*hworkm(i)+ ahworkf*wagef(i)*hworkf(i) + aq*consumption(i))/(wagem(i)*workh + wagef(i)*workh + nonlabor(i)),...
        scalem(i) == (privateleisurem(i)*wagem(i) + privatemhworkm(i)*wagem(i) + privatefhworkm(i)*wagef(i) + privateqM(i) + aleisurem*wagem(i)*leisurem(i) +  aleisuref*wagef(i)*leisuref(i) + ahworkm*wagem(i)*hworkm(i)+ ahworkf*wagef(i)*hworkf(i) + aq*consumption(i))/(wagem(i)*workh + wagef(i)*workh + nonlabor(i))];
    end
end


%% Specifying Objective Function
u = sdpvar(num_of_couples,1);         % vector of parameters necessasry to specify the function
Objective1 = sum(u.*scale);           % objective function that uses parameter -- u would be using as +/- unit vector 
Objective2 = sum(u.*scalem);          % objective function that uses parameter -- u would be using as +/- unit vector 
Objective3 = sum(u.*scalef);          % objective function that uses parameter -- u would be using as +/- unit vector 


% Solving the problem
options = sdpsettings('verbose',0,'solver','gurobi');
Opt1 = optimizer(Constraints,Objective1,options,u,scale); 
Opt2 = optimizer(Constraints,Objective2,options,u,scalem); 
Opt3 = optimizer(Constraints,Objective3,options,u,scalef); 

% parametrizations of the problem
param = eye(num_of_couples,num_of_couples);

for i=1:num_of_couples
    % economies of scale
    [xvalue, errorcode] = Opt1(param(:,i));
    if errorcode~=0
        display('Something went wrong (min)');    
    end
    min_scale(i) = xvalue(i);
       
    [xvalue, errorcode] = Opt1(-param(:,i));
    if errorcode~=0
        display('Something went wrong (max)');
    end
    max_scale(i) = xvalue(i);
    
    % male RICEB
    [xvalue, errorcode] = Opt2(param(:,i));
    if errorcode~=0
        display('Something went wrong (min)');
    end
    min_scalem(i) = xvalue(i);
    
    [xvalue, errorcode] = Opt2(-param(:,i));
    if errorcode~=0
        display('Something went wrong (max)');
    end
    max_scalem(i) = xvalue(i);
    
    % female RICEB
    [xvalue, errorcode] = Opt3(param(:,i));
    if errorcode~=0
        display('Something went wrong (min)');
    end
    min_scalef(i) = xvalue(i);
    
    [xvalue, errorcode] = Opt3(-param(:,i));
    if errorcode~=0
        display('Something went wrong (max)');
    end
    max_scalef(i) = xvalue(i);
   
end

return